﻿using AutoMapper;
using Harmontronics.DataAccess.IOperate.Business;
using Harmontronics.DataAccess.Operate.Business;
using Harmontronics.IService;
using Harmontronics.IService.Business;
using Harmontronics.Model.Dto;
using Harmontronics.Model.Dto.Model;
using Harmontronics.Model.Entitys;
using Harmontronics.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Harmontronics.Service
{
    public class ProductionLineService : IProductionLineService
    {
        private readonly IProductionLineOperate _productionLineOperate;
        private readonly IWorkShopOperate _workShopOperate;
        private readonly IMapper _mapper;
        private readonly IUnitOfWork _unitOfWork;
        public ProductionLineService(IUnitOfWork unitOfWork, IMapper mapper, IProductionLineOperate productionLineOperate, IWorkShopOperate workShopOperate)
        {
            _mapper = mapper;
            _unitOfWork = unitOfWork;
            _productionLineOperate = productionLineOperate;
            _workShopOperate = workShopOperate;
        }
        public ProductionLineModel Mending(ProductionLine entity)
        {
            ProductionLineModel model = _mapper.Map<ProductionLineModel>(entity);
            WorkShop workshop = _workShopOperate.GetWorkShopById(entity.WorkShopId);
            if (workshop != null)
            {
                WorkShopModel workShopModel = _mapper.Map<WorkShopModel>(workshop);
                model.workShop = workShopModel;
            }
            else
            {
                model.workShop = new WorkShopModel()
                {
                    WorkShopId = entity.WorkShopId,
                };
            }
            return model;
        }
        public List<ProductionLineModel> MendingRange(List<ProductionLine> entities)
        {
            List<ProductionLineModel> models = new List<ProductionLineModel>();
            foreach (ProductionLine entity in entities)
            {
                models.Add(Mending(entity));
            }
            return models;
        }
        public List<ProductionLine> GetAllProductionLine()
        {
            try
            {
                return _productionLineOperate.GetAllProductionLineList();
            }
            catch (Exception ex)
            {
                return new List<ProductionLine>();
            }
        }
        public List<ProductionLineModel> GetAllProductionLineModel()
        {
            try
            {
                List<ProductionLineModel> models = new List<ProductionLineModel>();
                List<ProductionLine> entities = _productionLineOperate.GetAllProductionLineList();
                foreach (var entity in entities)
                {
                    models.Add(Mending(entity));
                }
                return models;
            }
            catch (Exception ex)
            {
                return new List<ProductionLineModel>();
            }
        }
        public bool AddProductionLine(ProductionLine productionLine)
        {
            try
            {
                _productionLineOperate.AddProductionLine(productionLine);
                _unitOfWork.Commit();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool DeleteProductionLine(string id)
        {
            try
            {
                ProductionLine entity = _productionLineOperate.GetProductionLineById(id);
                if (entity != null)
                {
                    _productionLineOperate.DeleteProductionLine(entity);
                    _unitOfWork.Commit();
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }


        public ProductionLineModel GetProductionLineById(string id)
        {
            try
            {
                ProductionLine entity = _productionLineOperate.GetProductionLineById(id);
                if (entity != null)
                {
                    return Mending(entity);
                }
            }
            catch (Exception ex)
            {

            }
            return null;
        }


        public List<ProductionLineModel> GetProductionLineByWorkShopId(string id)
        {
            try
            {
                List<ProductionLine> entities = _productionLineOperate.GetProductionLineByWorkShopId(id);
                List<ProductionLineModel> models = new List<ProductionLineModel>();
                foreach (ProductionLine entity in entities)
                {
                    models.Add(Mending(entity));
                }
                return models;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public bool UpdateProductionLine(ProductionLine model)
        {
            try
            {
                ProductionLine entity = _productionLineOperate.GetProductionLineById(model.ProductionLineId);
                if (entity != null)
                {
                    _mapper.Map(model, entity);
                    _productionLineOperate.UpdateProductionLine(entity);
                    _unitOfWork.Commit();
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool BatchDelProductionLine(List<string> ids)
        {
            try
            {
                List<ProductionLine> entities = _productionLineOperate.GetProductionLineByIdList(ids);
                if (entities.Count > 0)
                {
                    _productionLineOperate.BatchDelProductionLine(entities);
                    _unitOfWork.Commit();
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}
